import { useComDesignStoreWithOut } from '@/store'
import { ref, unref, computed, watch } from 'vue'

import {
  BwCustomComNodeAttrDataTypeEnum as BwAttrDataTypeEnum,
  type BwCustomComNodeIfInfoModel,
  type BwCustomComNodeForInfoModel,
} from '@bitwit/base-com-render'
import { BwJsExtndInfoModel } from '../../../../../coms/JSExtend/model'
export function useSeniorValue(props, emit) {
  const designStore = useComDesignStoreWithOut()

  const curConstEle = ref<any>(null)
  const curVisible = ref<boolean>(false)
  const curConstValue = ref<any>('')
  const curComVarName = ref<string>('')
  const curComParamName = ref<string>('')
  const curJSExpression = ref<Record<string, any>>({})
  const curConstEleProps = ref<Record<string, any>>({})
  const curConstEleEmits = ref<Record<string, Function>>({})

  /** 当前自定义组件外放属性列表 */
  const curCurtomComPropList = computed(() => {
    const list: Array<Record<string, string>> = []
    const designInfo = designStore.curDesignInfo
    const comProps = designInfo?.comProps

    if (Array.isArray(comProps) && comProps.length) {
      comProps.forEach((item) => {
        list.push({ label: item.name ?? '', value: item.key })
      })
    }
    return list
  })

  const curConstEleBindProps = computed(() => {
    const options = unref(curConstEleProps)
    const com = props.valueDataType
    switch (com) {
      case BwAttrDataTypeEnum.array: {
        options.value = unref(curConstValue)
        break
      }
      case BwAttrDataTypeEnum.boolean: {
        options.checked = unref(curConstValue)
        break
      }
    }

    return options
  })

  /** 更新JS表达式 */
  const onChangeJSExpression = (data: BwJsExtndInfoModel) => {
    const curValue: Record<string, any> = {
      jsExpParams: data.params,
      jsExpBodyLogic: data.logic,
    }

    onChangeSeniosValue(curValue)
  }

  /** 修改组件节点高级配置值 */
  const onChangeSeniosValue = (value: any) => {
    emit('change', value)
  }

  watch(
    () => props.valueDataType,
    (newVal: BwAttrDataTypeEnum) => {
      const emits: Record<string, Function> = {}
      switch (newVal) {
        case BwAttrDataTypeEnum.boolean: {
          const curData: BwCustomComNodeIfInfoModel = props.info

          curConstEle.value = 'ASwitch'
          curConstEleProps.value = {}
          curConstValue.value = curData.constValue
          curComVarName.value = curData.comVarName ?? ''
          curComParamName.value = curData.comPropsName ?? ''
          curJSExpression.value = {
            params: curData.jsExpParams,
            logic: curData.jsExpBodyLogic,
          }

          emits['update:checked'] = (value) => {
            curConstValue.value = value
            onChangeSeniosValue(value)
          }
          break
        }

        case BwAttrDataTypeEnum.array: {
          const curData: BwCustomComNodeForInfoModel = props.info

          curConstEle.value = 'ATextarea'
          curConstEleProps.value = {}
          curConstValue.value = curData.constValue
          curComVarName.value = curData.comVarName ?? ''
          curComParamName.value = curData.comPropsName ?? ''
          curJSExpression.value = {
            params: curData.jsExpParams,
            logic: curData.jsExpBodyLogic,
          }
          emits['update:value'] = (value) => {
            const curValue = Array.isArray(value)
              ? JSON.stringify(value)
              : value.toString()

            curConstValue.value = value
            onChangeSeniosValue(curValue)
          }
          break
        }
        default: {
          curConstValue.value = ''
          curConstEle.value = 'ATextarea'
          curConstEleProps.value = { disabled: false }
        }
      }

      curConstEleEmits.value = emits
    },
    { deep: true, immediate: true }
  )

  return {
    curVisible,
    curConstEle,
    curComVarName,
    curComParamName,
    curJSExpression,
    curConstEleEmits,
    curConstEleBindProps,
    curCurtomComPropList,

    onChangeSeniosValue,
    onChangeJSExpression,
  }
}
